﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ANLP.Parser
{
    internal enum Tokens : int
    {
        Whitespace,
        DoublePercent,
        Code,
        MultipartToken,
        Token,
        SinglePercent,
        Colon,
        Pipe,
        SemiColon,
        Comma,
    }

    public class ParserLexer : ANLP.LexerBase
    {
        protected override void Initialize()
        {
            IDictionary<ANLP.LexerHandler, ANLP.TokenHandler> normalExpressions = new Dictionary<ANLP.LexerHandler, ANLP.TokenHandler>();
            IDictionary<ANLP.LexerHandler, ANLP.TokenHandler> codeDuplication = new Dictionary<ANLP.LexerHandler, ANLP.TokenHandler>();
            //IDictionary<Func<string, LexLocation>, Func<int>> codeExpression = new Dictionary<Func<string, LexLocation>, Func<int>>();
            lexExpressions.Add(0, normalExpressions);
            lexExpressions.Add(1, codeDuplication);
            //lexExpressions.Add(2, codeExpression);
            AddRegex(normalExpressions, @"\G%%", delegate() { return (int)Tokens.DoublePercent; });
            AddRegex(normalExpressions, @"\G%", delegate() { return (int)Tokens.SinglePercent; });
            AddRegex(normalExpressions, @"\G;", delegate() { return (int)Tokens.SemiColon; });
            AddRegex(normalExpressions, @"\G:", delegate() { return (int)Tokens.Colon; });
            AddRegex(normalExpressions, @"\G\|", delegate() { return (int)Tokens.Pipe; });
            AddRegex(normalExpressions, @"\G,", delegate() { return (int)Tokens.Comma; });
            AddRegex(normalExpressions, @"\G\{([^{}]*|\{[^}]+\})*\}", delegate() { return (int)Tokens.Code; });
            AddRegex(normalExpressions, @"\G(([A-Za-z][A-Za-z0-9]+)\.)+([A-Za-z][A-Za-z0-9]+)", delegate() { return (int)Tokens.MultipartToken; });
            AddRegex(normalExpressions, @"\G[A-Za-z_][A-Za-z0-9_-]+", delegate() { return (int)Tokens.Token; });

            AddRegex(codeDuplication, @"\G([^%]|%[^%])+", delegate() { return (int)Tokens.Code; });
            AddRegex(codeDuplication, @"\G%%", delegate() { return (int)Tokens.DoublePercent; });
            AddRegex(normalExpressions, @"\G[ \t\r\n\f\v]", delegate() { return (int)Tokens.Whitespace; });
            //AddRegex(codeExpression, @"\G[ \t\r\f\v]", delegate() { return (int)Tokens.Whitespace; });

        }
    }
}
